package cn.gzmli.dtt.common.workflow.service;

import cn.gzmli.dtt.common.entity.PageInfo;
import cn.gzmli.dtt.common.workflow.dao.FlowNodeDao;
import cn.gzmli.dtt.common.workflow.entity.FlowNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class FlowNodeService {

	@Autowired
	private FlowNodeDao flowNodeDao;

	public void addFlowNode(FlowNode flowNode) throws Exception {
		flowNodeDao.insert(flowNode);
	}

	public void changeFlowNode(FlowNode flowNode) throws Exception {
		flowNodeDao.update(flowNode);
	}
	
	public void deleteFlowNode(FlowNode flowNode) throws Exception {
		flowNodeDao.delete(flowNode);
	}

	public List<FlowNode> queryFlowNodesByCondition(Map condition) throws Exception {
		return flowNodeDao.queryEntity(condition);
	}

	public List<FlowNode> queryFlowNodesByPage(Map condition, PageInfo pageInfo) throws Exception {
		return flowNodeDao.queryEntityByPage(condition, pageInfo);
	}
	
	public List<Map<String,Object>> queryFlowNodeAll()throws Exception {
		String sql = "select id,id AS realID,name,workFlowID as pId,'FlowNode' as nodeType from flowNode where nodeType=6 order by nodeOrder asc";
		return flowNodeDao.getJdbcTemplate().queryForList(sql);
	}
	
	public List<Map<String,Object>> queryFlowNodeForUserMessage(Map<String, String> condition, PageInfo pageInfo, boolean isBind , Integer userID)throws Exception {
		StringBuilder sql = new StringBuilder();
		if(isBind){
			sql.append("SELECT t.* FROM"
					+ "(SELECT f.*,w.name as workFlowName,um.id as umID,um.userID,um.reciveState,um.sendState,um.dealState,um.status "
					+ " FROM flownode f "
					+ " INNER JOIN usermessage um ON um.flowNodeID=f.id AND um.userID="+userID+""
					+ " LEFT JOIN workflow w ON f.workFlowID=w.id)t "
					+ " WHERE 1=1 ");
		}else{
			sql.append("SELECT t.* FROM "
					+ " (SELECT f.*,w.name as workFlowName,um.id as umID,um.userID,um.reciveState,um.sendState,um.dealState,um.status "
					+ " FROM"
					+ " (SELECT * FROM flowNode "
					+ " WHERE id NOT IN (SELECT flowNodeID FROM usermessage WHERE userID="+userID+")"
					+ " )f "
					+ " LEFT JOIN usermessage um ON f.id=um.flowNodeID"
					+ " LEFT JOIN workflow w ON f.workFlowID=w.id)t"
					+ " WHERE 1=1 ");
		}
		for (Map.Entry<String, String> entry : condition.entrySet()) {
    		sql.append(entry.getKey()).append(entry.getValue());
		}
		String countSql=sql.toString().replace("SELECT t.* FROM","SELECT COUNT(*) totalRows FROM");
    	pageInfo.setTotalRows(flowNodeDao.getJdbcTemplate().queryForObject(countSql, Integer.class));
    	pageInfo.createTotalPages();
    	sql.append(" Limit "+((pageInfo.getCurPage()-1)*pageInfo.getPageSize())+","+pageInfo.getPageSize());
		return flowNodeDao.getJdbcTemplate().queryForList(sql.toString());
	}

	public List<Map<String, Object>> getTree() throws Exception {
		List<Map<String, Object>> dataList = new ArrayList<>();
		dataList.addAll(this.queryFlowNodeAll());

		final String sql = "SELECT id,name,'WorkFlow' AS nodeType FROM workflow";
		dataList.addAll(this.flowNodeDao.getJdbcTemplate().queryForList(sql));
		return dataList;
	}
	
	/**
	 * 根据流程id获取流程节点id
	 * @param workFlowID
	 * @return
	 * @throws Exception
	 */
	public List<Map<String,Object>> querFlowNodesByWorkFlowID(String workFlowID)throws Exception {
		StringBuffer sql=new StringBuffer("SELECT ");
		sql.append("wf.id workFlowID,wf.name workFlowName,wf.descript workFlowDescript,");
		sql.append("fn.id id,fn.workFlowID workFlowID,fn.name name,");
		sql.append("fn.x x ,fn.y y,fn.textX textX,fn.textY textY,");
		sql.append("fn.width width ,fn.height height,");
		sql.append("fn.nodeType nodeType,fn.objectScope,fn.orgScope,");
		sql.append("fn.nodeOrder nodeOrder,fn.performType performType,fn.fromRect fromRect,");
		sql.append("fn.toRect toRect,fn.createTime createTime,");
		sql.append(" GROUP_CONCAT(DISTINCT nor.orgID) orgIDS,GROUP_CONCAT(DISTINCT nor.roleID) roleIDS,GROUP_CONCAT(DISTINCT nu.userID) userIDS,");
		sql.append("fn.createTime FROM FlowNode fn ");
		sql.append("LEFT JOIN NodeOrgRole nor ON (fn.id=nor.flowNodeID) ");
		sql.append("LEFT JOIN nodeUser nu ON (fn.id=nu.flowNodeID) ");
		sql.append("LEFT JOIN WorkFlow wf ON (fn.workFlowID=wf.id) ");
		sql.append("WHERE workFlowID='"+workFlowID+"' GROUP BY fn.id ");
		return flowNodeDao.getJdbcTemplate().queryForList(sql.toString());
	}
}	
